package com.easyaccount.service;

import com.easyaccount.bean.MenuTree;
import com.easyaccount.bean.RoleMenuTree;
import com.easyaccount.commons.CommonResp;
import com.easyaccount.commons.TablePage;
import com.easyaccount.dao.DailyAcctDictMapper;
import com.easyaccount.dao.MenuMapper;
import com.easyaccount.dao.RoleMapper;
import com.easyaccount.dao.RoleMenuMapper;
import com.easyaccount.exception.BizException;
import com.easyaccount.model.DailyAcctDict;
import com.easyaccount.model.Menu;
import com.easyaccount.model.DailyAcctDict;
import com.easyaccount.model.RoleMenu;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
public class DailyAcctDictService {

    @Resource
    private DailyAcctDictMapper dailyAcctDictMapper;

    public CommonResp save(DailyAcctDict dailyAcctDict){
        int count = dailyAcctDictMapper.selectByIdAndNameCount(dailyAcctDict);
        if (count > 0) throw new BizException("已存在",444);

        int i = dailyAcctDictMapper.insertSelective(dailyAcctDict);
        if (i == 0) throw new BizException("添加失败",444);
        return CommonResp.success();
    }

    public CommonResp list(){
        List<DailyAcctDict> dailyAcctDicts = dailyAcctDictMapper.selectAll();
        return CommonResp.success(dailyAcctDicts);
    }

    public TablePage searchByName(TablePage tablePage, String name){
        int size = tablePage.getLimit();
        int start = (tablePage.getPage() - 1) * size;

        List<DailyAcctDict> roles = dailyAcctDictMapper.selectByName(start, size, name);

        int count = dailyAcctDictMapper.selectByNameCount(name);
        tablePage.setCount(count);
        tablePage.setData(roles);
        return tablePage;
    }

    public CommonResp update(DailyAcctDict dailyAcctDict){
        int count = dailyAcctDictMapper.selectByIdAndNameCount(dailyAcctDict);
        if (count > 0) throw new BizException("已存在",444);

        int n = dailyAcctDictMapper.updateByPrimaryKeySelective(dailyAcctDict);
        if (n < 1)  throw new BizException("修改失败",444);

        return CommonResp.success();
    }


    public CommonResp delete(DailyAcctDict dailyAcctDict){
        DailyAcctDict result = dailyAcctDictMapper.selectByPrimaryKey(dailyAcctDict.getId());
        if(result == null) throw new BizException("不存在",444);

        int i = dailyAcctDictMapper.deleteByPrimaryKey(dailyAcctDict.getId());
        if (i == 0) throw new BizException("删除失败",444);

        return CommonResp.success();
    }
}
